#include "executorTask.h"

THData LimitData = {0};

void showContrl(void);
void alarmContrl(void);
void airContrl(void);
void curtainContrl(void);

void executeInit(void)
{
    if (LimitData.state == 0)
    {
        // 需要加载阈值数据--读eeprom,现在先模拟
        LimitData.state = 15; // 1111
        LimitData.tempMax = 200;
        LimitData.tempMin = -20;
        LimitData.humiMax = 200;
        LimitData.humiMin = 20;
        LimitData.sunshine = 100;
        LimitData.smoke = 200;
    }
    // 外设初始化
    BEEP_Init();
    OLED_Init();
    IIC_Init();
}

void executorTaskTask(void *param)
{

    while (1)
    {
        printf("temp--->%d\r\n", HandledData.temp);
        printf("humi--->%d\r\n", HandledData.humi);
        printf("sunshine--->%d\r\n", HandledData.sunshine);
        printf("smoke--->%d\r\n", HandledData.smoke);
        printf("rain--->%d\r\n", HandledData.raidrop);
        u8 contral = LimitData.state;
        if (contral & 0x01)
        {
            showContrl();
        }
        if (contral & 0x02)
        {
            alarmContrl();
        }
        if (contral & 0x04)
        {
            airContrl();
        }
        if (contral & 0x08)
        {
            curtainContrl();
        }
        vTaskDelay(20);
    }
}

void executorTaskCreate(void)
{

    // 创建一个启动任务
    BaseType_t result = xTaskCreate(
        executorTaskTask,
        EXECUTOR_TASK_NAME,
        EXECUTOR_TASK_STACK_DEPTH,
        NULL,
        EXECUTOR_TASK_PRIORITY,
        NULL);

    if (result == pdPASS)
        printf("create executorTask task sucess!\r\n");
    else
        printf("create executorTask task fail!\r\n");
}

void showContrl()
{
    // printf("temp--->%d\r\n", HandledData.temp);
    // printf("humi--->%d\r\n", HandledData.humi);
    // printf("sunshine--->%d\r\n", HandledData.sunshine);
    // printf("smoke--->%d\r\n", HandledData.smoke);
    // printf("rain--->%d\r\n", HandledData.raidrop);
    OLED_ShowString(0, 0, "temp:", 10);
    OLED_ShowString(0, 15, "temp:", 10);
    OLED_Refresh_Gram();
    OLED_ShowNum(30, 0, HandledData.temp, 2, 10);
    OLED_ShowNum(30, 15, HandledData.humi, 2, 10);
    OLED_Refresh_Gram();
}

void alarmContrl()
{
    u8 beepflag = 0;
    if (HandledData.temp > LimitData.tempMax || HandledData.temp < LimitData.tempMin)
    {
        beepflag = 1;
        LED2 = !LED2;
    }

    if (HandledData.humi > LimitData.humiMax || HandledData.humi < LimitData.humiMin)
    {
        beepflag = 1;
        LED3 = !LED3;
    }

    if (HandledData.sunshine > LimitData.sunshine)
    {
        beepflag = 1;
        LED4 = !LED4;
    }

    if (HandledData.smoke > LimitData.smoke)
    {
        beepflag = 1;
        LED5 = !LED5;
    }

    if (HandledData.raidrop == 0)
    {
        beepflag = 1;
        LED6 = !LED6;
    }

    if (beepflag)
    {
        BEEP = !BEEP;
    }
    else
    {
        BEEP = 1;
    }
}

void airContrl()
{
}

void curtainContrl()
{
}
